package com.cool.modules.finance.controller.admin;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.cool.core.annotation.CoolRestController;
import com.cool.core.base.BaseController;
import com.cool.core.util.CoolSecurityUtil;
import com.cool.modules.base.entity.sys.table.BaseSysUserEntityTableDef;
import com.cool.modules.finance.constant.OrderType;
import com.cool.modules.finance.entity.account.table.FinanceAccountEntityTableDef;
import com.cool.modules.finance.entity.category.table.FinanceCategoryEntityTableDef;
import com.cool.modules.finance.entity.order.FinanceOrderEntity;
import com.cool.modules.finance.entity.order.FinanceOrderImgEntity;
import com.cool.modules.finance.entity.order.table.FinanceOrderEntityTableDef;
import com.cool.modules.finance.service.family.FinanceUserFamilyService;
import com.cool.modules.finance.service.order.FinanceOrderImgService;
import com.cool.modules.finance.service.order.FinanceOrderService;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Tag(name = "账目详细", description = "账目详细")
@CoolRestController(api = { "add", "delete", "update", "list","page" })
@RequiredArgsConstructor
public class AdminFinanceOrderController extends BaseController<FinanceOrderService, FinanceOrderEntity> {


    final FinanceOrderImgService orderImgService;

    final FinanceUserFamilyService userFamilyService;

    @Override
    protected void init(HttpServletRequest request, JSONObject requestParams) {

        FinanceOrderEntity bean = requestParams.toBean(FinanceOrderEntity.class);
        Date[] dateRange = CollectionUtil.isNotEmpty(bean.getDateRange())?bean.getDateRange().toArray(Date[]::new):new Date[0];
        FinanceOrderEntityTableDef order = FinanceOrderEntityTableDef.FINANCE_ORDER_ENTITY.as("finance_order");
        FinanceAccountEntityTableDef account = FinanceAccountEntityTableDef.FINANCE_ACCOUNT_ENTITY.as("a");
        FinanceCategoryEntityTableDef category = FinanceCategoryEntityTableDef.FINANCE_CATEGORY_ENTITY.as("c");
        BaseSysUserEntityTableDef sysUser = BaseSysUserEntityTableDef.BASE_SYS_USER_ENTITY.as("sysuser");
        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.select(order.ALL_COLUMNS,account.ACCOUNT_NAME.as(FinanceOrderEntity::getAccountName),category.CATEGORY_NAME.as(FinanceOrderEntity::getCategoryName),sysUser.NAME.as(FinanceOrderEntity::getCreateUserName)).
                from(order).
                leftJoin(account).on(order.ACCOUNT_ID.eq(account.ID)).
                leftJoin(category).on(order.CATEGORY_ID.eq(category.ID)).
                leftJoin(sysUser).on(order.CREATE_USER.eq(sysUser.ID)).
                where(order.ORDER_TYPE.eq(bean.getOrderType()).when(bean.getOrderType()!=null)).
                and(order.CATEGORY_ID.eq(bean.getCategoryId()).when(bean.getCategoryId()!=null)).
                and(order.ACCOUNT_ID.eq(bean.getAccountId()).when(bean.getAccountId()!=null)).
                and(order.ORDER_DETAIL.like("%"+bean.getOrderDetail()+"%").when(StringUtils.isNotBlank(bean.getOrderDetail()))).
                and(order.IS_DAILY.eq(bean.getIsDaily()).when(bean.getIsDaily()!=null)).
                and(order.ORDER_DATE.between(dateRange).when(CollectionUtil.isNotEmpty(bean.getDateRange()))).
                and(order.FAMILY_ID.eq(userFamilyService.getFamilyIdByUserId(CoolSecurityUtil.getCurrentUserId()))).
                and(order.COIN_ID.eq(bean.getCoinId()).when(bean.getCoinId()!=null)).
                and(order.CREATE_USER.eq(CoolSecurityUtil.getCurrentUserId()).when(bean.isSelf()));

        setPageOption(createOp().keyWordLikeFields(order.ORDER_DETAIL).queryWrapper(queryWrapper).transform(o->{
            FinanceOrderEntity foe = (FinanceOrderEntity) o;
            List<String> list = orderImgService.list(new QueryWrapper().eq(FinanceOrderImgEntity::getOrderId, ((FinanceOrderEntity) o).getId())).stream().map(FinanceOrderImgEntity::getImgPath).toList();
            foe.setImageList(list);
        }));
    }


    @Override
    protected FinanceOrderEntity buildSubData(QueryWrapper queryWrapper) {
        FinanceOrderEntity entity = new FinanceOrderEntity();
        List<FinanceOrderEntity> list = service.list(queryWrapper);
        BigDecimal outcome = list.stream().filter(x->x.getOrderType().equals(OrderType.EXPEND)).map(FinanceOrderEntity::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal income = list.stream().filter(x -> x.getOrderType().equals(OrderType.INCOME)).map(FinanceOrderEntity::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        entity.setOutCome(outcome);
        entity.setInCome(income);
        return entity;
    }
}
